package com.jhs.boot.system.controller;

import com.jhs.boot.system.model.vo.GraphNodeVO;
import com.jhs.boot.system.model.vo.NodesThirdVO;
import com.jhs.boot.system.service.SysNodesTrafficService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jhs.boot.system.model.form.SysNodesTrafficForm;
import com.jhs.boot.system.model.query.SysNodesTrafficQuery;
import com.jhs.boot.system.model.vo.SysNodesTrafficVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jhs.boot.common.result.PageResult;
import com.jhs.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 节点流量前端控制层
 *
 * @author zxy
 * @since 2025-08-11 19:21
 */
@Tag(name = "节点流量接口")
@RestController
@RequestMapping("/api/v1/sys-nodes-traffic")
@RequiredArgsConstructor
public class SysNodesTrafficController  {

    private final SysNodesTrafficService sysNodesTrafficService;

    @Operation(summary = "节点流量分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('ap:nodesTraffic:query')")
    public PageResult<SysNodesTrafficVO> getSysNodesTrafficPage(SysNodesTrafficQuery queryParams ) {
        IPage<SysNodesTrafficVO> result = sysNodesTrafficService.getSysNodesTrafficPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增节点流量")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('ap:nodesTraffic:add')")
    public Result<Void> saveSysNodesTraffic(@RequestBody @Valid SysNodesTrafficForm formData ) {
        boolean result = sysNodesTrafficService.saveSysNodesTraffic(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取节点流量表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('ap:nodesTraffic:edit')")
    public Result<SysNodesTrafficForm> getSysNodesTrafficForm(
        @Parameter(description = "节点流量ID") @PathVariable Long id
    ) {
        SysNodesTrafficForm formData = sysNodesTrafficService.getSysNodesTrafficFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改节点流量")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('ap:nodesTraffic:edit')")
    public Result<Void> updateSysNodesTraffic(
            @Parameter(description = "节点流量ID") @PathVariable Long id,
            @RequestBody @Validated SysNodesTrafficForm formData
    ) {
        boolean result = sysNodesTrafficService.updateSysNodesTraffic(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除节点流量")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('ap:nodesTraffic:delete')")
    public Result<Void> deleteSysNodesTraffics(
        @Parameter(description = "节点流量ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sysNodesTrafficService.deleteSysNodesTraffics(ids);
        return Result.judge(result);
    }

    @Operation(summary = "获取所有某MPP MAP节点")
    @GetMapping("/getAllNodesTraffic")
    public Result<List<NodesThirdVO>> getAllNodesTraffic( @RequestParam("id") Long id,
                                                          @RequestParam("timeRange") String timeRange) {
        List<NodesThirdVO> list = sysNodesTrafficService.getAllNodes(id,timeRange);
        return Result.success(list);
    }
}
